package com.bs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.codehaus.jettison.json.JSONObject;
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.*;
import com.bs.bean.*;
import com.bs.service.AdminService;
import com.bs.utils.*;
/*控制层：AdminController*/
@RestController
@RequestMapping(value="/json/admin",produces="application/json; charset=utf-8")
public class AdminJsonController {
//	注入AdminService对象
	@Autowired
	private AdminService adminService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpSession session;
	@Autowired
	private ServletContext context;
//	查询数据返回到/page/admin/adminItem.jsp页面
	@RequestMapping("/findAll")
	public Result findAll(Admin admin, @RequestParam(required = false, defaultValue = "true") Boolean mode) {
		try {
			List<Admin> list = adminService.findAll(admin,mode);
			return Result.success().put("list",list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
		
	}
	@RequestMapping("/findById")
	public Result findById(String username) {
		try {
			Admin admin = adminService.findById(username);
			return Result.success().put("admin",admin);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
//	删除数据
	public void deleteById(String usernameString) {
		if (usernameString!=null) {
			String username = String.valueOf(usernameString);
			//	删除数据库中admin中的对应username的数据
			adminService.deleteById(username);
		}
	}
//	根据adminItem.jsp页面传递的id，调用adminServlet#deleteById(id)方法删除admin表中的数据，重定向到findAll请求
	@RequestMapping("/deleteById")
	public Result deleteById() {
//		单条删除
		deleteById(request.getParameter("username"));
//		批量删除
		String usernames = request.getParameter("usernames");
		if (usernames!=null) {
			for (String username : usernames.split(",")) {
				deleteById(username);
			}
		}
			return Result.success();
	}
	@RequestMapping("/delete")
	public Result delete(Admin admin) {
		try {
			adminService.delete(admin);
			return Result.success("删除成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
	@RequestMapping("/deleteByIds")
	public Result deleteByIds(List<String> list) {
		try {
			adminService.deleteByIds(list);
			return Result.success("删除成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
	@RequestMapping("/updateByIdSelective")
	public Result updateByIdSelective(Admin admin) {
		try {
			adminService.updateByIdSelective(admin);
			return Result.success("更新成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
	
//	根据adminUpdate.jsp页面传递的数据，更新数据库，重定向到findAll请求
	@RequestMapping("/updateById")
	public Result updateById(Admin admin) {
		try {
			adminService.updateByIdSelective(admin);
		} catch (Exception e) {
			e.printStackTrace();
		}
			return Result.success();
	}

//	根据adminInsert.jsp页面传递的数据，封装在admin对象中插入数据库，重定向到重定向到findAll请求
	@RequestMapping("/insert")
	public Result insert(Admin admin) {
		try {
			adminService.insert(admin);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
			return Result.success();
	}
}